package com.inforich.app.api.controller.orderManage;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.app.api.dto.BookingAllocationDTO;
import com.inforich.app.api.dto.TransportDTO;
import com.inforich.app.api.service.IAppTransportService;
import com.inforich.app.component.LoginUser;
import com.inforich.app.component.LoginUserApp;
import com.inforich.app.component.PreAppRole;
import com.inforich.appManage.service.IContactService;
import com.inforich.common.annotation.RepeatSubmit;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.orderManage.domain.Booking;
import com.inforich.orderManage.domain.Transport;
import com.inforich.orderManage.service.ITransportService;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 运输单controller
 * @author: HJM
 * @date: 2022/10/31
 */
@RestController
@RequestMapping("/h5api/orderManage/transport")
public class AppTransportController {

  @Autowired
  private IAppTransportService appTransportService;
  @Autowired
  private IContactService contactService;
  @Autowired
  private ITransportService transportService;

  /**
   * 公司运输单(分页列表)
   */
  @PreAppRole("transport_company_admin")
  @GetMapping("/getCompanyTransportList")
  public Page<Transport> getCompanyTransportList(@LoginUser LoginUserApp loginUserApp,
      Page<Transport> page) {
    // 公司ID
    String transportCompanyId = contactService.getTransportCompanyIdByAppUserId(
        loginUserApp.getId());
    if (StrUtil.isBlank(transportCompanyId)) {
      return page;
    }
    Map<String, Object> map = new HashMap<>();
    map.put("transportCompanyId", transportCompanyId);
    transportService.pageTransport(page, map);
    return page;
  }

  /**
   * 司机运输单(分页列表)
   */
  @PreAppRole("driver")
  @GetMapping("/getDriverTransportList")
  public Page<Transport> getCompanyBookingList(@LoginUser LoginUserApp loginUserApp,
      Page<Transport> page) {
    // 司机ID
    Long driverId = contactService.getDriverIdByAppUserId(loginUserApp.getId());
    if (driverId == null) {
      return page;
    }
    Map<String, Object> map = new HashMap<>();
    map.put("driverId", driverId);
    transportService.pageTransport(page, map);
    return page;
  }

  /**
   * 查询详情
   */
  @GetMapping("/query")
  public AjaxResult getInfo(String id) {
    return AjaxResult.success(appTransportService.getInfo(id));
  }

  /**
   * 获取运输状态
   */
  @PreAppRole("driver")
  @PostMapping("/getTransportStatus")
  public AjaxResult getTransportStatus(@LoginUser LoginUserApp loginUserApp,
      @RequestBody TransportDTO dto) {
    return appTransportService.getTransportStatus(loginUserApp, dto);
  }

  /**
   * 开始发车
   */
  @PreAppRole("driver")
  @PostMapping("/startTransport")
  @RepeatSubmit
  public AjaxResult startTranspoapprt(@LoginUser LoginUserApp loginUserApp,
      @Validated @RequestBody TransportDTO dto) {
    return appTransportService.startTransport(loginUserApp, dto);
  }

  /**
   * 运输结束
   */
  @PreAppRole("driver")
  @PostMapping("/endTransport")
  @RepeatSubmit
  public AjaxResult endTransport(@LoginUser LoginUserApp loginUserApp,
      @Validated @RequestBody TransportDTO dto) {
    return appTransportService.endTransport(loginUserApp, dto);
  }
}
